package TreeStructure;

public class Tree<T> {
	private TreeNode<T> root;
	private int count = 0;
	private String parentLink;
	private String foundparentLink;
	private boolean gasit = false;

	@SuppressWarnings("unchecked")
	public String getFoundparentLink() {
		getFirstParentLink((TreeNode<MyNode>) root);
		gasit = false;
		return foundparentLink;
	}

	public void setFoundparentLink(String foundparentLink) {
		this.foundparentLink = foundparentLink;
	}

	public String getParentLink() {
		return parentLink;
	}

	public void setParentLink(String parentLink) {
		this.parentLink = parentLink;
	}

	public Tree() {

	}

	public Tree(TreeNode<T> root) {
		this.root = root;
	}

	public TreeNode<T> AddNode(T data) {
		TreeNode<T> newNode = new TreeNode<T>(data);
		if (root == null) {
			root = newNode;
			root.setParent(null);
		}
		count = 1;
		return root;
	}

	public TreeNode<T> AddNode(T data, TreeNode<T> parent) {
		if (parent == null) {
			return AddNode(data);
		}
		TreeNode<T> newNode = new TreeNode<T>(data);
		parent.getChildren().add(newNode);
		newNode.setParent(parent);
		count++;
		return newNode;
	}

//	public void CallgetFirstParentLink() {
//		getFirstParentLink((TreeNode<MyNode>) root);
//	}

	// searches in the tree for the node that has the given real link
	public void getFirstParentLink(TreeNode<MyNode> node) {
		if (!gasit) {
			if (("/" + node.getData().getLinkReal()).equals(getParentLink())) {
				setFoundparentLink(node.getFirstLevelParent().getData().getLinkReal());
				gasit = true;
			}
			for (int i = 0; i < node.getChildren().size(); i++) {
				getFirstParentLink(node.getChildren().get(i));
			}
		}
	}

	public TreeNode<T> getRoot() {
		return root;
	}

	public void setRoot(TreeNode<T> root) {
		this.root = root;
	}

	public void Clear() {
		root = null;
		count = 0;
	}

	public int getCount() {
		return count;
	}

	public void setCount(int count) {
		this.count = count;
	}
}
